<?php
/**
 * 提货产品汇总
 * (model)
 */
namespace App\Models\Shipping;

use App\Models\Inventory\StorageDetail;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;
class SSkuTotal extends Model
{
    protected $table = 's_shipping_total';

    protected $guarded = [];

    public static $_isLock = [
        0 => '未锁定',
        1 => '已锁定',
    ];

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    /**
     * 获取当期采购到货数量(国内收货入库数量)
     * @param $sDate
     * @param $eDate
     * @param $mPSkuId
     * @return mixed
     */
    public static function getStorageNum($sDate,$eDate,$mPSkuId){
        return StorageDetail::query()
            ->whereHas('storage',function ($query) use($sDate,$eDate){
                $query->where('type', 1) //收货入库
                    ->where('storage_at','>=',$sDate)
                    ->where('storage_at','<',$eDate);
            })
            ->where('psku_id',$mPSkuId)
            ->sum('storage_num');
    }
}
